package com.example.lenovo.demo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.lenovo.demo.presenter.adapter.HomeMyRvAdapter;
import com.example.lenovo.demo.presenter.adapter.Main_VpAdapter;
import com.example.lenovo.demo.view.CityHotFragment;
import com.example.lenovo.demo.view.CityMapFragment;
import com.example.lenovo.demo.view.FgManager;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener,DrawerLayout.DrawerListener{
    private static boolean isQuit=false;
    private Timer timer=new Timer();
    @ViewInject(R.id.main_tb)
    private Toolbar toolbar;
    @ViewInject(R.id.main_vp)
    private ViewPager viewPager;
    @ViewInject(R.id.main_tblayout)
    private   TabLayout tabLayout;
    public static DrawerLayout drawerLayout;

    public static LinearLayout linearLayout;
    public static FrameLayout frameLayout;
    private List<Fragment> lists;
    private String[] arr;
    private Main_VpAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        drawerLayout= (DrawerLayout) findViewById(R.id.main_drawerlayout);
        drawerLayout.addDrawerListener(this);
        drawerLayout.setVisibility(View.GONE);
        init();
    }
    /**
     * 初始化控件
     * */
    private void init() {
        setSupportActionBar(toolbar);
        frameLayout= (FrameLayout) findViewById(R.id.activity_main);
        linearLayout= (LinearLayout) findViewById(R.id.main_linearlayout);
        arr=getResources().getStringArray(R.array.arr);
        lists=new ArrayList<>();
        adapter=new Main_VpAdapter(getSupportFragmentManager(),lists,arr);
        initToolBar();
        initViewPager();

    }
    /**
     * 初始化ViewPager相关设置
     * */
    private void initViewPager() {
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(4);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnPageChangeListener(this);
    }

    /**
     * 初始化ToolBar相关设置
     * */
    private void initToolBar() {
        tabLayout.setTabTextColors(Color.parseColor("#FFFFFF"),Color.parseColor("#FFFFFF"));
        tabLayout.setSelectedTabIndicatorHeight(4);
        tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
        for (int i = 0; i < arr.length; i++) {
            lists.add(FgManager.getFragment(arr[i]));
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /**
     * 加载另外的Fragment时，顶部ToolBar不再隐藏
     * */
    @Override
    public void onPageSelected(int position) {
        MainActivity.linearLayout.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));


    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    /**
     * 按下返回键时判断当前显示是否为CityFragment，是则remove并show HomeFragment
     * */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
//            List<Fragment> list=getSupportFragmentManager().getFragments();
//            Toast.makeText(this,list.size()+"个Fragment在显示",Toast.LENGTH_SHORT).show();
//            for (Fragment fragment : list) {
//                if(fragment instanceof CityHotFragment){
//                    FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
//                    // CityRvAdapter.PathVH.radioGroup.removeAllViews();
//                    transaction.remove(fragment);
//                    transaction.show(HomeMyRvAdapter.homeFragment).commit();
//                    MainActivity.linearLayout.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));
//                    MainActivity.drawerLayout.setVisibility(View.GONE);
//                }
//                if(fragment instanceof CityMapFragment){
//                    FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
//                    // CityRvAdapter.PathVH.radioGroup.removeAllViews();
//                    transaction.remove(fragment).commit();
//                   // transaction.show(HomeMyRvAdapter.homeFragment).commit();
//                 //   MainActivity.linearLayout.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));
//                  //  MainActivity.drawerLayout.setVisibility(View.GONE);
//                }
//            }
             if(isQuit==false){
                isQuit=true;
                Toast.makeText(getApplicationContext(), "再次按下返回键退出应用程序!", Toast.LENGTH_LONG).show();

                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isQuit=false;
                    }
                },2000);
            }else {
                System.exit(0);
            }

        }

            return false;

        }


    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        MainActivity.linearLayout.animate().translationY(- MainActivity.linearLayout.getHeight()/2).setInterpolator(new AccelerateInterpolator(2));

    }

    @Override
    public void onDrawerOpened(View drawerView) {

    }

    @Override
    public void onDrawerClosed(View drawerView) {
        MainActivity.drawerLayout.setVisibility(View.GONE);
        List<Fragment> list=getSupportFragmentManager().getFragments();
        for (Fragment fragment : list) {
            if(fragment instanceof CityHotFragment){
                FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
                transaction.remove(fragment).commit();
                MainActivity.linearLayout.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));
                MainActivity.drawerLayout.setVisibility(View.GONE);
            }
        }


    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }
}
